package com.workpaper.app.workpaper.daily.view;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.AppConsts;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.consts.workpaper.DailyWorkpaperViewConsts;
import com.workpaper.app.utils.platform.SWTPlatformUtils;
import com.workpaper.app.workpaper.daily.business.DailyWorkpaperBusiness;
import com.workpaper.app.workpaper.daily.entity.DailyWorkpaper;

public class DailyWorkpaperEditDialog extends Dialog {
	/**
	 * 日期选择器
	 */
	private DateTime dateSelector;
	/**
	 * 内容文本
	 */
	private StyledText contentText;
	/**
	 * 所有人
	 */
	private Text masterText; 
	/**
	 * 备注
	 */
	private StyledText remarkText;
	/**
	 * Id 标签，在编辑数据时，缓存当前对象的id
	 */
	private Label idLabel;
	
	/**
	 * 待编辑工作日报数据
	 */
	private DailyWorkpaper dailyWorkpaper;
	
	public DailyWorkpaperEditDialog(Shell parentShell) {
		super(parentShell);
	}

	public DailyWorkpaperEditDialog(Shell parentShell, DailyWorkpaper dailyWorkpaper) {
		super(parentShell);
		this.dailyWorkpaper = dailyWorkpaper;
	}
	
	/**
	 * 配置窗口标题、图标等信息
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.WPD_Add_Title));	//标题
//		newShell.setImage(null);	//图标
	}

	@Override
	protected Point getInitialSize() {
		return new Point(400, 400);
	}

	/**
	 * 创建界面
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 6;
		container.setLayout(layout);

		//创建用日期：标签、日期控件 输入框
		Label dsNameLabel = new Label(container, SWT.NONE);
		dsNameLabel.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.WPD_EditText_Date));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		dsNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		dsNameLabel.setImage(CacheImage.getAppImage(ImagePath.LOGIN_USERNAME_ICO));
		dateSelector = new DateTime(container, SWT.DATE);
		dateSelector.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		//创建内容：
		Label contentLabel = new Label(container, SWT.NONE);
		contentLabel.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.WPD_EditText_Content));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		contentLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		contentText = new StyledText(container, SWT.BORDER);
		contentText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		//创建所有人：
		Label executorLabel = new Label(container, SWT.NONE);
		executorLabel.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.WPD_EditText_Master));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		executorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		masterText = new Text(container, SWT.BORDER);
		masterText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		//创建备注：
		Label remarkLabel = new Label(container, SWT.NONE);
		remarkLabel.setText(ViewInfoCache.get(DailyWorkpaperViewConsts.WPD_EditText_Remark));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		remarkLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		remarkText = new StyledText(container, SWT.BORDER);
		remarkText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		
		//id标签
		idLabel = new Label(container, SWT.NONE);
		idLabel.setVisible(false);
		
		if(dailyWorkpaper != null) {
			setEditData();
			dailyWorkpaper = null;
		}
		return container;
	}

	/**
	 * 设置要编辑的数据
	 */
	private void setEditData() {
		Date worDate = dailyWorkpaper.getWorkDate();
		Calendar wdCalendar = Calendar.getInstance();
		wdCalendar.setTime(worDate);
		dateSelector.setYear(wdCalendar.get(Calendar.YEAR));
		dateSelector.setMonth(wdCalendar.get(Calendar.MONTH));
		dateSelector.setDay(wdCalendar.get(Calendar.DAY_OF_MONTH));
		
		contentText.setText(dailyWorkpaper.getContent());
		
		masterText.setText(dailyWorkpaper.getMaster());
		
		remarkText.setText(dailyWorkpaper.getRemark());
		
		idLabel.setData(dailyWorkpaper.getId());
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		//保存按钮
		createButton(parent, IDialogConstants.OK_ID, ViewInfoCache.get(CommonViewConsts.ButtonText_Save), false);
		//取消按钮，默认选中
		createButton(parent, IDialogConstants.CANCEL_ID, ViewInfoCache.get(CommonViewConsts.ButtonText_Cancel), true);
	}


	@Override
	protected void okPressed() {
		DailyWorkpaper workpaper = extractEditData();
		DailyWorkpaperManagerView dailyWorkpaperManagerView = SWTPlatformUtils.getViewByID(AppConsts.DAILY_WORKPAPER_MANAGE_VIEW);
		if(StringUtils.isBlank(workpaper.getId())) {
			//新增
			DailyWorkpaperBusiness.getInstance().addOneWorkpaperDaily(workpaper);
			dailyWorkpaperManagerView.refreshTableData();
		}else {
			DailyWorkpaperBusiness.getInstance().updateOne(workpaper);
			dailyWorkpaperManagerView.refreshTableData();
		}
		super.okPressed();
	}
	
	/**
	 * 提取编辑的数据
	 * @return
	 */
	private DailyWorkpaper extractEditData() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, dateSelector.getDay());
		calendar.set(Calendar.MONTH, dateSelector.getMonth());
		calendar.set(Calendar.YEAR, dateSelector.getYear());
		
		Date planDate = calendar.getTime();
		String master = masterText.getText();
		String content = contentText.getText();
		String remark = remarkText.getText();
		String id = (String) idLabel.getData();
		
		DailyWorkpaper workpaper = new DailyWorkpaper();
		workpaper.setContent(content);
		workpaper.setMaster(master);
		workpaper.setWorkDate(planDate);
		workpaper.setRemark(remark);
		workpaper.setId(id);
		return workpaper;
	}


	@Override
	protected void cancelPressed() {
		super.cancelPressed();
	}

}
